
package checker;

import readers.QueryStringHandler;
import readers.Reader;
import readers.UserReader;
import validator.*;

/**
 * Checks whether an user exists
 * @author Chris
 */
public class checkerIsUser extends checker {
    public checkerIsUser() { err = ERRORS.VALID; }
    
    public boolean isValid (String query) {
        QueryStringHandler qsh = new QueryStringHandler(query);
        String userName = qsh.getElement("username");
        String password = qsh.getElement("password"); //Needed for compatiblity
        return isValid(userName, password);
    }
    
    //Still have password to maintain compatibility
    public boolean isValid(String userName, String password) {
        validator val;
        
        val = new validUserName();
        //Just check it is a valid name and then check if the file exists
        if(!val.isValid(userName) || !Reader.fileExists(Reader.readLocation + userName+"_password.txt")) {
            err = ERRORS.INVALID_USERNAME;
            return false;
        }
        err = Errors.ErrorInformation.ERRORS.VALID;
        return true;
    }
}
